<template>
  <q-page padding>
    <div class="row">
      <!-- 左侧文件栏 -->
      <div class="sidebar">
        <div class="file-list">
          <div>
            <div
              v-for="(file, index) in files"
              :key="index"
              class="file-item"
              :class="{ selected: selectedXlsxName === file.file_name }"
              @click="selectFile(file.file_name)"
            >
              {{ file.file_name }}
              <q-icon
                name="download"
                class="delete-icon"
                color="red"
                @click.stop="handleDownloadFile(file)"
              >
                <q-tooltip class="bg-red" :offset="[10, 10]"> 下载 </q-tooltip>
              </q-icon>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧图表生成组件 -->
      <div style="width: 80vw; height: 88vh">
        <PlotComponent
          v-if="selectedXlsxName"
          :project_id="projectId"
          :file_id="fileId"
          :xlsx_name="selectedXlsxName"
        />
      </div>
    </div>
  </q-page>
</template>

<script setup>
import { ref, onMounted } from "vue";
import PlotComponent from "src/components/Draw/Drawing.vue"; // 子组件路径
import { getDrawData } from "src/api/api";

// 选中的xlsx_name
const selectedXlsxName = ref("");
const projectId = ref(2); // 假设的project_id
const fileId = ref(7); // 假设的file_id
const files = ref([]); // 文件列表

// 从后端获取文件列表
const fetchFiles = async () => {
  try {
    const response = await getDrawData({
      project_id: projectId.value,
      file_id: fileId.value,
    });
    files.value = response.data;
    console.log(files);
  } catch (error) {
    console.error("Failed to fetch files:", error);
  }
};

// 选中文件并传递给子组件
const selectFile = (fileName) => {
  selectedXlsxName.value = fileName;
};

// 下载文件
const handleDownloadFile = async (file) => {
  try {
    const response = await fetch(
      import.meta.env.VITE_BACKEND_URL + file.file_path
    );
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    const blob = await response.blob();
    const url = URL.createObjectURL(blob);

    const a = document.createElement("a");
    a.href = url;
    a.download = file.file_name; // 文件名
    document.body.appendChild(a); // Append to the DOM
    a.click(); // Trigger the download
    document.body.removeChild(a); // Remove the element
    URL.revokeObjectURL(url); // Clean up the URL object
  } catch (error) {
    console.error("Download failed:", error);
  }
};

// 在组件挂载时获取文件列表
onMounted(() => {
  fetchFiles();
});
</script>

<style scoped>
.q-page {
  display: flex;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.sidebar {
  border: 1px solid #ddd;
  border-radius: 8px;
  background-color: #ffffff;
  padding: 16px;
  width: 15vw;
  height: 88vh;
}

.file-list {
  max-height: 400px; /* 限制文件列表的高度 */
  overflow-y: auto; /* 使文件列表可以滚动 */
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.file-item:hover {
  background-color: #e0f7fa;
}

.file-item.selected {
  /* 选中状态的样式 */
  background-color: #b2ebf2; /* 可以根据需要调整颜色 */
  font-weight: bold; /* 选中时加粗文件名 */
}

.delete-icon {
  cursor: pointer;
}
</style>
